package com.fingard.dsp.bank.directbank.fuiou01;

import java.util.HashMap;
import java.util.Map;

import com.fingard.crypto.DigestHelper;
import com.fingard.dsp.bank.directConfig.ActSetItem;
import com.fingard.dsp.bank.directbank.bankInterface.IBalance;
import com.fingard.dsp.bank.reqrespobj.balance.Req7505HisBalQry;
import com.fingard.dsp.bank.reqrespobj.balance.Req7506BalQry;
import com.fingard.dsp.bank.reqrespobj.balance.RetAts7505HisBalQry;
import com.fingard.dsp.bank.reqrespobj.balance.RetAts7506BalQry;
import com.fingard.dsp.bank.reqrespobj.balance.detail.RetAtsBalDetail;
import com.fingard.text.StringHelper;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

/**
 * 富友余额查询
 * 
 * @author Cruise
 * @date 2018/11/04
 */
public class FUIOU01Balance extends FUIOU01Base implements IBalance {
	@Override
	public RetAts7506BalQry d_7506_BalQry(Req7506BalQry p_req7506) throws Exception {
		RetAts7506BalQry retAts = new RetAts7506BalQry(p_req7506.reqHeader);
		RetAtsBalDetail tmpRetDetail = new RetAtsBalDetail();
		retAts.writeStart();

		try {
			while (p_req7506.readRecord()) {
				String ownAct = p_req7506.readedDetail.actNum;
				ActSetItem actItem = getBankConfig().getActSetItem(ownAct, "");
				if (actItem != null) {
					FUIOU01Header bankHeader = new FUIOU01Header();
					bankHeader.reqtype = "qryacnt";

					Map<String, String> postParams = new HashMap<String, String>();
					postParams.put("merid", actItem.merchantId);
					postParams.put("reqtype", bankHeader.reqtype);
					
					String reqStr = "1.0|" + actItem.merchantId + "|" + actItem.saltKey;
					String mac = DigestHelper.cryptoMD5RetHexStr(reqStr.getBytes(getCharset()));
					
					XmlTextWriter xmlWriter = new XmlTextWriter();
					xmlWriter.writeStartDocByDeclaration("<?xml version=\"1.0\" encoding=\"" + getCharset() + "\" standalone=\"yes\"?>");
					xmlWriter.writeStartElement("qryacnt");
					xmlWriter.writeElementString("ver", "1.0");
					xmlWriter.writeElementString("merid", actItem.merchantId);
					xmlWriter.writeElementString("mac", mac);
					xmlWriter.writeEndElement();
					xmlWriter.writeEndDocument();
					
					postParams.put("xml", xmlWriter.toXmlString());
					postParams.put("mac", mac);

					String[] tmpStrRet = sendToBank("qryBalance", postParams);
					
					if (tmpStrRet[0].length() == 0) {
						StringBuilder tmpSbMsg = new StringBuilder();
						XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
						if (tmpBkReader != null) {
							String tmpRespCode = tmpBkReader.getFirstText("/qryacnt/ret");
							String tmpRespInfo = tmpBkReader.getFirstText("/qryacnt/memo");
							if ("000000".equals(tmpRespCode)) {
								tmpRetDetail.respCode = "0";
								tmpRetDetail.respInfo = tmpRespInfo;
								tmpRetDetail.actArea = p_req7506.readedDetail.actArea;
								tmpRetDetail.actCur = p_req7506.readedDetail.actCur;
								tmpRetDetail.actNum = p_req7506.readedDetail.actNum;
								tmpRetDetail.currBal = StringHelper.amountDivide100(tmpBkReader.getFirstText("/qryacnt/ctamt"));
								tmpRetDetail.availBal = StringHelper.amountDivide100(tmpBkReader.getFirstText("/qryacnt/caamt"));
								tmpRetDetail.frozenBal = StringHelper.amountDivide100(tmpBkReader.getFirstText("/qryacnt/cfamt"));
							} else {
								tmpRetDetail.respCode = "-1";
								tmpRetDetail.respInfo = tmpRespInfo;
							}
						} else {
							tmpRetDetail.respCode = "-1";
							tmpRetDetail.respInfo = tmpSbMsg.toString();
						}
					} else {
						tmpRetDetail.respCode = "-1";
						tmpRetDetail.respInfo = tmpStrRet[1];
					}
				} else {
					tmpRetDetail.respCode = "-1";
					tmpRetDetail.respInfo = "未找到账号[" + ownAct + "]商户配置信息!";
				}
			}
			retAts.writeRetDetail(tmpRetDetail, p_req7506.isBatch);
		} catch (Exception ex) {
			ex.printStackTrace();
			retAts.writeRespCode("-1");
			retAts.writeRespInfo(ex.getMessage());
			WriteBankLogLn(ex);
		}

		retAts.writeEnd();
		return retAts;
	}

	@Override
	public RetAts7505HisBalQry d_7505_BalQry(Req7505HisBalQry p_req7505) throws Exception {
		return null;
	}
}
